<template>
	<view class="content"> 
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
			<button @click="login">登陆</button>
			<button @click="getUserInfo">获取用户信息</button>
		</view>
		
	</view>
</template>

<script>
	import {login,getUserInfo} from '../../api/user.js'
	import store from '@/store';
	
	import Cache from '../../utils/cache';
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {
			this.login()
		},
		methods: {
			async getUserInfo(){
				const res = await getUserInfo()
				console.log('用户信息',res)
			},
			login(){
				let appid = store.getters.appid
				uni.login({
					complete:async (res) =>{
						let post_data = {
							code:res.code,
							appid:appid,
							clickid:"", //触点ID
							invite_code:"", //上级邀请码
							agent_admin_id:"", //代理商管理员ID
							advertiser_id:"", //广告主ID
							projectid_id:"", //项目ID
							promotion_id:"", //计划ID
						}
						const result = await login(post_data)
						let expires_time = result.data.expires_time
						// 缓存用户信息
						store.commit('UPDATE_USERINFO',result.data)
						// 缓存token 与token过期时间
						store.commit('LOGIN',{
							token:result.data.token,
							time:expires_time - this.$Cache.time(),
						})
						
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
